package com.exporter.ui;

import java.awt.Color;
import java.text.NumberFormat;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.exporter.listeners.AdvancedOptionsListener;
import com.exporter.listeners.ComboboxSelectionListener;
import com.exporter.listeners.DestinationFolderActionListener;
import com.exporter.listeners.ExportFilesActionListener;
import com.exporter.listeners.RadioButtonActionListener;
import com.exporter.listeners.SourceFolderActionListener;
import com.exporter.listeners.SourceFolderDcumentListener;
import com.exporter.listeners.ToggleSftpOptionListener;
import com.exporter.listeners.ZipOptionListener;
import com.exporter.utility.ConfigurationLoader;

public class MainWindow extends JFrame
{
	private static final long serialVersionUID = 1L;
	private JLabel lblSourceFolder;
	private JCheckBox chkbxLogFile;
	private JLabel lblUptoDate;
	private JComboBox comboToDate;
	private JComboBox comboExportFromDate;
	private JComboBox comboFileOfDate;
	private JRadioButton radioExportFilesOfDate;
	private JRadioButton radioExportFromDate;
	private JCheckBox chkbxEnableAdvOptions;
	private JPanel advancedOptionsPanel;
	private JPanel sprSftpDetails;
	private JTextField txtUploadPath;
	private JLabel lblUploadPath;
	private JPasswordField txtPassword;
	private JLabel lblPassword;
	private JTextField txtUserName;
	private JLabel lblUserName;
	private JTextField txtSftpPort;
	private JLabel lblsftpPort;
	private JTextField txtSftpHost;
	private JLabel lblSftpHost;
	private JButton btnExport;
	private JCheckBox chkbxSftp;
	private JCheckBox chkbxZipFile;
	private JTextArea txtarFileIgnore;
	private JCheckBox chkbxFolderStructure;
	private JScrollPane fileIgnoreScrollPane;
	private JLabel lblFileIgnroe;
	private JTextArea txtarFolderIgnore;
	private JScrollPane folderIgnoreScrollPane;
	private JLabel lblFolderIgnore;
	private JButton btnDestFolder;
	private JTextField txtDestFolder;
	private JLabel lblDestFolder;
	private JButton btnSourceFolder;
	private JTextField txtSourceFolder;
	private ButtonGroup radioBtnGroup;
	private TitledBorder advancedBorder;
	private TitledBorder border;

	{
		// Set Look & Feel
		try
		{
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void initGUI()
	{
		{
			getContentPane().setLayout(null);
			this.setTitle("Changed Files Exporter v1.1.1");
			this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			{
				lblSourceFolder = new JLabel();
				getContentPane().add(lblSourceFolder);
				lblSourceFolder.setText("Workspace folder path");
				lblSourceFolder.setBounds(10, 11, 120, 14);
			}
			{
				txtSourceFolder = new JTextField();
				getContentPane().add(txtSourceFolder);
				txtSourceFolder.setBounds(130, 8, 254, 20);
				txtSourceFolder.setEditable(false);
				txtSourceFolder.setBackground(Color.white);
			}
			{
				btnSourceFolder = new JButton();
				getContentPane().add(btnSourceFolder);
				btnSourceFolder.setText(". . .");
				btnSourceFolder.setBounds(390, 7, 51, 23);
				btnSourceFolder.addActionListener(new SourceFolderActionListener(this, this.txtSourceFolder));
			}
			{
				lblDestFolder = new JLabel();
				getContentPane().add(lblDestFolder);
				lblDestFolder.setText("Destination folder path");
				lblDestFolder.setBounds(10, 36, 116, 14);
			}
			{
				txtDestFolder = new JTextField();
				getContentPane().add(txtDestFolder);
				txtDestFolder.setBounds(130, 33, 254, 20);
				txtDestFolder.setEditable(false);
				txtDestFolder.setBackground(Color.white);
				txtDestFolder.setToolTipText(txtDestFolder.getText());
			}
			{
				btnDestFolder = new JButton();
				getContentPane().add(btnDestFolder);
				btnDestFolder.setText(". . .");
				btnDestFolder.setBounds(390, 32, 51, 23);
				btnDestFolder.addActionListener(new DestinationFolderActionListener(this, txtDestFolder));
			}
			{
				lblFolderIgnore = new JLabel();
				getContentPane().add(lblFolderIgnore);
				lblFolderIgnore.setText("Folder ignore pattern ( ex: .settings, META-INF etc.)");
				lblFolderIgnore.setBounds(10, 61, 256, 14);
			}
			{
				folderIgnoreScrollPane = new JScrollPane();
				getContentPane().add(folderIgnoreScrollPane);
				folderIgnoreScrollPane.setBounds(10, 81, 431, 35);
				folderIgnoreScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				folderIgnoreScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
				{
					txtarFolderIgnore = new JTextArea();
					// txtarFolderIgnore.setText(
					// ".settings,.metadata,.jazz3,classes,bin,build" );
					txtarFolderIgnore.setText(ConfigurationLoader.FOLDER_IGNORE);
					folderIgnoreScrollPane.setViewportView(txtarFolderIgnore);
					txtarFolderIgnore.setPreferredSize(new java.awt.Dimension(414, 33));
				}
			}
			{
				lblFileIgnroe = new JLabel();
				getContentPane().add(lblFileIgnroe);
				lblFileIgnroe.setText("File ignore pattern ( ex: .class, .svn etc.)");
				lblFileIgnroe.setBounds(10, 127, 200, 14);
			}
			{
				fileIgnoreScrollPane = new JScrollPane();
				getContentPane().add(fileIgnoreScrollPane);
				fileIgnoreScrollPane.setBounds(10, 147, 431, 37);
				fileIgnoreScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				fileIgnoreScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
				{
					txtarFileIgnore = new JTextArea();
					// txtarFileIgnore.setText(
					// ".class,.svn,.classpath,.project,.xmi,.db" );
					txtarFileIgnore.setText(ConfigurationLoader.FILE_IGNORE);
					fileIgnoreScrollPane.setViewportView(txtarFileIgnore);
				}
			}
			{
				chkbxLogFile = new JCheckBox();
				getContentPane().add(chkbxLogFile);
				chkbxLogFile.setSelected(true);
				chkbxLogFile.setText("Generate log file");
				chkbxLogFile.setBounds(6, 326, 107, 23);
			}
			{
				chkbxFolderStructure = new JCheckBox();
				getContentPane().add(chkbxFolderStructure);
				chkbxFolderStructure.setText("Keep folder structure");
				chkbxFolderStructure.setBounds(115, 326, 129, 23);
			}
			{
				sprSftpDetails = new JPanel();
				getContentPane().add(sprSftpDetails);
				border = new TitledBorder("SFTP Details");
				sprSftpDetails.setBorder(border);
				sprSftpDetails.setBounds(10, 356, 431, 108);
				sprSftpDetails.setEnabled(false);
				sprSftpDetails.setLayout(null);
				{
					lblSftpHost = new JLabel();
					sprSftpDetails.add(lblSftpHost);
					lblSftpHost.setText("Host");
					lblSftpHost.setBounds(9, 25, 22, 14);
					lblSftpHost.setEnabled(false);
				}
				{
					txtSftpHost = new JTextField();
					sprSftpDetails.add(txtSftpHost);
					txtSftpHost.setBounds(43, 22, 173, 20);
					txtSftpHost.setEnabled(false);
				}
				{
					lblsftpPort = new JLabel();
					sprSftpDetails.add(lblsftpPort);
					lblsftpPort.setText("Port");
					lblsftpPort.setBounds(236, 25, 20, 14);
					lblsftpPort.setEnabled(false);
				}
				{
					txtSftpPort = new JFormattedTextField(NumberFormat.getIntegerInstance());
					sprSftpDetails.add(txtSftpPort);
					txtSftpPort.setBounds(269, 22, 146, 20);
					txtSftpPort.setEnabled(false);
				}
				{
					lblUserName = new JLabel();
					sprSftpDetails.add(lblUserName);
					lblUserName.setText("Username");
					lblUserName.setBounds(11, 53, 48, 14);
					lblUserName.setEnabled(false);
				}
				{
					txtUserName = new JTextField();
					sprSftpDetails.add(txtUserName);
					txtUserName.setBounds(68, 50, 148, 20);
					txtUserName.setEnabled(false);
				}
				{
					lblPassword = new JLabel();
					sprSftpDetails.add(lblPassword);
					lblPassword.setText("Password");
					lblPassword.setBounds(236, 52, 46, 14);
					lblPassword.setEnabled(false);
				}
				{
					txtPassword = new JPasswordField();
					sprSftpDetails.add(txtPassword);
					txtPassword.setBounds(292, 49, 123, 20);
					txtPassword.setEnabled(false);
				}
				{
					lblUploadPath = new JLabel();
					sprSftpDetails.add(lblUploadPath);
					lblUploadPath.setText("File Upload Path");
					lblUploadPath.setBounds(11, 83, 77, 14);
					lblUploadPath.setEnabled(false);
				}
				{
					txtUploadPath = new JTextField();
					sprSftpDetails.add(txtUploadPath);
					txtUploadPath.setBounds(96, 80, 319, 20);
					txtUploadPath.setEnabled(false);
				}
				border.setTitleColor(Color.gray);
			}
			{
				chkbxSftp = new JCheckBox();
				getContentPane().add(chkbxSftp);
				chkbxSftp.setText("Upload to SFTP");
				chkbxSftp.setBounds(342, 326, 99, 23);
				chkbxSftp.setEnabled(false);
				chkbxSftp.addItemListener(new ToggleSftpOptionListener(sprSftpDetails, lblSftpHost, txtSftpHost, lblsftpPort, txtSftpPort, lblUserName, txtUserName, lblPassword, txtPassword,
						lblUploadPath, txtUploadPath, border));
			}
			{
				chkbxZipFile = new JCheckBox();
				getContentPane().add(chkbxZipFile);
				chkbxZipFile.setText("Make a zip file");
				chkbxZipFile.setBounds(246, 326, 94, 23);
				chkbxZipFile.addItemListener(new ZipOptionListener(chkbxSftp));
			}
			{
				advancedOptionsPanel = new JPanel();
				getContentPane().add(advancedOptionsPanel);
				advancedBorder = new TitledBorder("Advanced Export Options");
				advancedOptionsPanel.setBorder(advancedBorder);
				advancedOptionsPanel.setBounds(10, 216, 431, 103);
				advancedOptionsPanel.setEnabled(false);
				advancedOptionsPanel.setLayout(null);
				{
					radioExportFromDate = new JRadioButton();
					advancedOptionsPanel.add(radioExportFromDate);
					radioExportFromDate.setText("Export all files from the date ");
					radioExportFromDate.setBounds(16, 17, 182, 23);
					radioExportFromDate.setEnabled(false);
				}
				{
					radioExportFilesOfDate = new JRadioButton();
					advancedOptionsPanel.add(radioExportFilesOfDate);
					radioExportFilesOfDate.setText("Export files of a specific date");
					radioExportFilesOfDate.setBounds(16, 69, 182, 23);
					radioExportFilesOfDate.setEnabled(false);
				}
				{
					radioBtnGroup = new ButtonGroup();
					radioBtnGroup.add(radioExportFromDate);
					radioBtnGroup.add(radioExportFilesOfDate);
				}
				{
					comboFileOfDate = new JComboBox();
					advancedOptionsPanel.add(comboFileOfDate);
					comboFileOfDate.setBounds(214, 70, 186, 20);
					comboFileOfDate.setEnabled(false);
				}
				{
					comboToDate = new JComboBox();
					advancedOptionsPanel.add(comboToDate);
					comboToDate.setBounds(214, 44, 186, 20);
					comboToDate.setEnabled(false);
				}
				{
					comboExportFromDate = new JComboBox();
					advancedOptionsPanel.add(comboExportFromDate);
					comboExportFromDate.setBounds(214, 18, 186, 20);
					comboExportFromDate.setEnabled(false);
					comboExportFromDate.addActionListener(new ComboboxSelectionListener(comboExportFromDate, comboToDate));
				}
				{
					lblUptoDate = new JLabel();
					advancedOptionsPanel.add(lblUptoDate);
					lblUptoDate.setText("Upto date");
					lblUptoDate.setBounds(125, 47, 48, 14);
					lblUptoDate.setEnabled(false);
				}
				advancedBorder.setTitleColor(Color.gray);
			}
			{
				chkbxEnableAdvOptions = new JCheckBox();
				getContentPane().add(chkbxEnableAdvOptions);
				chkbxEnableAdvOptions.setText("Enable advanced export options");
				chkbxEnableAdvOptions.setBounds(10, 189, 181, 23);
				chkbxEnableAdvOptions.setEnabled(false);
				chkbxEnableAdvOptions.addItemListener(new AdvancedOptionsListener(radioExportFromDate, comboExportFromDate, lblUptoDate, comboToDate, radioExportFilesOfDate, comboFileOfDate,
						radioBtnGroup, txtSourceFolder, advancedBorder, txtarFolderIgnore, txtarFileIgnore,this));

			}
			{
				radioExportFromDate.addActionListener(new RadioButtonActionListener(radioBtnGroup, radioExportFromDate, radioExportFilesOfDate, comboExportFromDate, comboFileOfDate, lblUptoDate,
						comboToDate));
				radioExportFilesOfDate.addActionListener(new RadioButtonActionListener(radioBtnGroup, radioExportFromDate, radioExportFilesOfDate, comboExportFromDate, comboFileOfDate, lblUptoDate,
						comboToDate));
				txtSourceFolder.getDocument().addDocumentListener(new SourceFolderDcumentListener(chkbxEnableAdvOptions, this));
			}
			{
				btnExport = new JButton();
				getContentPane().add(btnExport);
				btnExport.setText("Export Files");
				btnExport.setBounds(352, 475, 89, 23);
				btnExport.addActionListener(new ExportFilesActionListener(txtSourceFolder, txtDestFolder, txtarFolderIgnore, txtarFileIgnore, chkbxFolderStructure, chkbxZipFile, chkbxSftp,
						txtSftpHost, txtSftpPort, txtUserName, txtPassword, txtUploadPath, this, chkbxEnableAdvOptions, radioExportFromDate, radioExportFilesOfDate, comboExportFromDate, comboToDate,
						comboFileOfDate,chkbxLogFile));
			}
		}
		{
			this.setSize(457, 535);
			this.setResizable(false);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		}
	}

	public MainWindow()
	{
		boolean configFlag = ConfigurationLoader.loadConfiguration();
		initGUI();
		if (!configFlag)
		{
			JOptionPane.showMessageDialog(this, "Configuration file not found \n Loading default configuration ");
		}
	}

}
